import nme.Assets;
import nme.display.Graphics;
import nme.display.DisplayObject;
import nme.display.Sprite;
import nme.display.Bitmap;
import nme.display.BitmapData;
import nme.display.Tilesheet;

import nme.geom.Rectangle;
import nme.geom.Matrix;
import nme.geom.Point;

import nme.events.Event;
import nme.events.MouseEvent;
import nme.events.KeyboardEvent;
import nme.Lib;

class Home extends Sprite implements IGameState
{
	public var _substate:SubState;
	public var _resourceRequest:Int;
	
	private var background:Sprite;
	private var moduleLayer:Sprite;
	private var texture:Bitmap;
	private var canvas:Sprite;
	private var anim:Animation;
	
	private var _time:Int = 0;
	private var _frameId:String = "";
	private var _currentFrame:Int = 0;
	private var _nFrame:Int = 0;
	
	public function new ()
	{
		super ();
		_substate = SubState.LOADING_STATE;		
	}
	
	public function getSubstate ():SubState
	{
		return _substate;
	}
	
	public function loading ():Void
	{
	}
	
	public function loadingCompleted ():Bool
	{
		return true;
	}
	
	public function init ():Void
	{
		_substate = SubState.LOOP_STATE;
		
		var column:Int = Std.int (stage.stageWidth / 10);
		var row:Int = Std.int (stage.stageHeight / 10);
		graphics.clear ();
		
		for (c in 0 ... column)
		{
			graphics.lineStyle (1, 0x00FF00, c % 10 == 0 ? 0.55 : 0.15);
			graphics.moveTo (c * 10, 0);
			graphics.lineTo (c * 10, stage.stageWidth);
		}
		
		for (r in 0 ... row)
		{
			graphics.lineStyle (1, 0x00FF00, r % 10 == 0 ? 0.55 : 0.15);
			graphics.moveTo (0, r * 10);
			graphics.lineTo (stage.stageWidth, r * 10);
		}
		
		var xml:Xml = Xml.parse(Assets.getText ("assets/cricket.xml")).firstElement ();
		var sprite:SpriteData = SpriteData.create (xml);
		var ms:Array<ModuleData> = new Array <ModuleData> ();
		
		var anim:Animation = new Animation (sprite, "3008");
		anim.x = 400;
		anim.y = 300;
		addChild (anim);
		
		var frame:Frame = new Frame (sprite, "2000");
		frame.x = 100;
		frame.y = 300;
		addChild (frame);
		
		var frameData:FrameData = sprite._frames.get ("2000");
		canvas = new Sprite ();
		canvas.x = 600;
		canvas.y = 600;
		canvas.graphics.clear();
		canvas.graphics.drawTiles (sprite._tileSheet, frameData.getTileData (), true, Tilesheet.TILE_TRANS_2x2);
		addChild (canvas);
	}

	private var isChange:Bool = true;
	public function loop ():Void
	{
		if (isChange)
		{
			isChange = false;
		}
	}
	
	public function destroy ():Void
	{
	}
	
	private function onkeyboard (e:KeyboardEvent):Void
	{
		// switch (e.keyCode)
		// {
		// }
	}
}